# -*- coding: utf-8 -*-
import sys
import io
import json
import requests

if sys.platform == 'win32':
    sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

target_version = "26.0.1"
build = "23A355"  # iOS/iPadOS Build
device_model = "iPhone16,2"

print(f"查询签名状态")
print(f"版本: {target_version}")
print(f"Build: {build}")
print(f"设备: {device_model}")
print("=" * 80)

# 尝试IPSW API (另一个常用的iOS固件数据源)
print(f"\n方法1: 使用IPSW.me API")
ipsw_url = f"https://api.ipsw.me/v4/device/{device_model}?type=ipsw"
print(f"URL: {ipsw_url}")

try:
    response = requests.get(ipsw_url, timeout=15)
    if response.status_code == 200:
        data = response.json()
        print(f"[OK] 获取设备信息成功")
        print(f"设备名称: {data.get('name', 'N/A')}")
        
        if 'firmwares' in data:
            firmwares = data['firmwares']
            print(f"\n找到 {len(firmwares)} 个固件")
            
            # 查找目标版本
            found = False
            for fw in firmwares:
                version = fw.get('version', '')
                if target_version in version or build in fw.get('buildid', ''):
                    found = True
                    print(f"\n[找到目标版本!]")
                    print(f"  版本: {fw.get('version', 'N/A')}")
                    print(f"  Build: {fw.get('buildid', 'N/A')}")
                    print(f"  签名状态: {'✓ 已开放' if fw.get('signed', False) else '✗ 已关闭'}")
                    print(f"  发布日期: {fw.get('releasedate', 'N/A')}")
                    print(f"  下载URL: {fw.get('url', 'N/A')[:80]}...")
                    break
            
            if not found:
                print(f"\n[未找到] 未找到版本 {target_version}")
                print(f"\n最新5个固件:")
                for fw in firmwares[:5]:
                    version = fw.get('version', 'N/A')
                    buildid = fw.get('buildid', 'N/A')
                    signed = fw.get('signed', False)
                    status = "[签名中]" if signed else "[已关闭]"
                    print(f"  - {version} ({buildid}) {status}")
        else:
            print(f"[警告] 响应中没有固件列表")
    else:
        print(f"[失败] HTTP {response.status_code}")
except Exception as e:
    print(f"[错误] {e}")

# 尝试另一个API端点
print(f"\n" + "=" * 80)
print(f"方法2: 使用appledb.dev的ota端点")

# 尝试查询OTA更新
ota_url = f"https://api.appledb.dev/ota/main.json"
print(f"URL: {ota_url}")

try:
    response = requests.get(ota_url, timeout=15)
    if response.status_code == 200:
        all_otas = response.json()
        print(f"[OK] 获取到 {len(all_otas)} 个OTA更新")
        
        # 搜索匹配的OTA
        matching_otas = []
        for ota in all_otas:
            version = str(ota.get('version', ''))
            build_id = str(ota.get('build', ''))
            if target_version in version or build in build_id:
                matching_otas.append(ota)
        
        if matching_otas:
            print(f"\n找到 {len(matching_otas)} 个匹配的OTA更新:")
            for ota in matching_otas[:10]:
                version = ota.get('version', 'N/A')
                build_id = ota.get('build', 'N/A')
                os_str = ota.get('osStr', 'iOS')
                unique_build = ota.get('uniqueBuild', build_id)
                
                print(f"\n  版本: {version} ({os_str})")
                print(f"  Build: {build_id}")
                print(f"  唯一标识: {unique_build}")
                print(f"  发布日期: {ota.get('released', 'N/A')}")
                
                # 尝试获取签名状态
                detail_url = f"https://api.appledb.dev/ota/{unique_build}.json"
                try:
                    detail_resp = requests.get(detail_url, timeout=5)
                    if detail_resp.status_code == 200:
                        detail_data = detail_resp.json()
                        if 'deviceMap' in detail_data:
                            device_map = detail_data['deviceMap']
                            if device_model in device_map:
                                device_info = device_map[device_model]
                                signed = device_info.get('signed', False)
                                print(f"  [{device_model}] 签名状态: {'✓ 已开放' if signed else '✗ 已关闭'}")
                except:
                    pass
        else:
            print(f"\n未找到匹配的OTA更新")
    else:
        print(f"[失败] HTTP {response.status_code}")
except Exception as e:
    print(f"[错误] {e}")

